<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="消息类型" prop="communicationEventTypeId">
        <el-select v-model="queryParams.communicationEventTypeId" placeholder="请选择消息类型" clearable>
          <el-option v-for="dict in communicationEventTypeIdOptions" :key="dict.enumCode" :label="dict.description"
            :value="dict.enumCode" />
        </el-select>
      </el-form-item>
      <el-form-item label="消息状态" prop="statusId">
        <el-input v-model="queryParams.statusId" placeholder="请输入消息状态" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="主题" prop="subject">
        <el-input v-model="queryParams.subject" placeholder="请输入主题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ehr:event:add']">新增</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="eventList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="communicationEventId" width="60" />
      <el-table-column label="消息类型" align="center" prop="communicationEventTypeId" width="100"
        :formatter="communicationEventTypeIdFormat" />
      <el-table-column label="发送人" align="center" prop="partyIdFrom" width="100" />
      <el-table-column label="接收人" align="center" prop="partyIdTo" width="100" :formatter="partyIdToFormat" />
      <el-table-column label="主题" align="center" prop="subject" width="120" />
      <el-table-column label="消息内容" align="center" prop="content" width="180" />
      <el-table-column label="接收时间" align="center" prop="datetimeStarted" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.datetimeStarted, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="消息状态" align="center" prop="statusId" width="100" />
      <!-- <el-table-column label="更新时间" align="center" prop="lastUpdatedStamp" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastUpdatedStamp, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document" @click="handleDetail(scope.row)"
            v-hasPermi="['ehr:event:detail']">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ehr:event:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['ehr:event:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改消息中心对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-bind:disabled=detailFlag>
        <el-row>
          <el-col :span="12">
            <el-form-item label="接收人" prop="partyIdTo">
              <el-select v-model="form.partyIdTo" placeholder="请选择接收人" filterable>
                <el-option v-for="dict in partyIdToOptions" :key="dict.partyId" :label="dict.firstName"
                  :value="dict.partyId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="消息类型" prop="communicationEventTypeId">
              <el-select v-model="form.communicationEventTypeId" placeholder="请选消息类型">
                <el-option v-for="dict in communicationEventTypeIdOptions" :key="dict.enumCode" :label="dict.description"
                  :value="dict.enumCode"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="主题" prop="subject">
              <el-input v-model="form.subject" placeholder="请输入主题" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="消息内容">
              <editor v-model="form.content" :min-height="192" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="发送人" prop="partyIdFrom">
              <el-input v-model="form.partyIdFrom" placeholder="请输入发送人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="statusId">
              <el-input v-model="form.statusId" placeholder="请输入状态" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
        </el-row>
        <!-- <el-row>
          <el-col :span="12">
            <el-form-item label="开始日期时间" prop="datetimeStarted">
              <el-date-picker clearable v-model="form.datetimeStarted" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                default-time="00:00:00" placeholder="请选择开始日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期时间" prop="datetimeEnded">
              <el-date-picker clearable v-model="form.datetimeEnded" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                default-time="00:00:00" placeholder="请选择结束日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row> -->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="editFlag" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEvent, getEvent, delEvent, addEvent, updateEvent } from "@/api/ehr/event";

export default {
  name: "Event",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消息中心表格数据
      eventList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 通信事件类型业务字典
      communicationEventTypeIdOptions: [],
      // 目标当事人业务字典
      partyIdToOptions: [],
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 操作标识,用于区分是详情
      detailFlag: false,
      // 操作标识,用于区分编辑状态
      editFlag: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "", // 排序字段
        isAsc: "", // 升序或降序 大写的 ASC或DESC,为空代表升序
        communicationEventTypeId: null,
        statusId: null,
        partyIdFrom: null,
        partyIdTo: null,
        datetimeStarted: null,
        datetimeEnded: null,
        subject: null,
        content: null,
        note: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.queryParams.partyIdTo = this.$store.state.user.partyId;
    // 通信事件类型业务字典
    this.getEnumDicts("COMM_EVENT_TYPE").then((response) => {
      this.communicationEventTypeIdOptions = response.data;
    });
    // 目标当事人业务字典
    this.getEntityDicts("person:partyId:firstName").then((response) => {
      this.partyIdToOptions = response.data;
    });
    this.getList();
  },
  methods: {
    communicationEventTypeIdFormat(row, column) {
      let val = null;
      this.communicationEventTypeIdOptions.forEach((item, index) => {
        if (row.communicationEventTypeId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    partyIdToFormat(row, column) {
      let val = null;
      this.partyIdToOptions.forEach((item, index) => {
        if (row.partyIdTo === item.partyId) {
          val = item.firstName;
        }
      });
      return val;
    },
    /** 查询消息中心列表 */
    getList() {
      this.loading = true;
      listEvent(this.queryParams).then(response => {
        this.eventList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        communicationEventId: null,
        communicationEventTypeId: null,
        caseId: null,
        origCommEventId: null,
        parentCommEventId: null,
        statusId: null,
        contactMechTypeId: null,
        contactMechIdFrom: null,
        contactMechIdTo: null,
        roleTypeIdFrom: null,
        roleTypeIdTo: null,
        partyIdFrom: null,
        partyIdTo: null,
        entryDate: null,
        datetimeStarted: null,
        datetimeEnded: null,
        subject: null,
        contentMimeTypeId: null,
        content: null,
        note: null,
        reasonEnumId: null,
        contactListId: null,
        headerString: null,
        fromString: null,
        toString: null,
        ccString: null,
        bccString: null,
        messageId: null,
        orgId: null,
        deptId: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.communicationEventId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.editFlag = true,
        this.updateFlag = false;
      this.detailFlag = false;
      this.reset();
      this.open = true;
      this.title = "添加消息中心";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.editFlag = false,
        this.updateFlag = false;
      this.detailFlag = true;
      this.reset();
      const communicationEventId = row.communicationEventId || this.ids
      getEvent(communicationEventId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "消息中心详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editFlag = true;
      this.updateFlag = true;
      this.detailFlag = false;
      this.reset();
      const communicationEventId = row.communicationEventId || this.ids
      getEvent(communicationEventId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改消息中心";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.communicationEventId != null && this.updateFlag) {
            updateEvent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEvent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const communicationEventIds = row.communicationEventId || this.ids;
      this.$modal.confirm('是否确认删除消息中心编号为"' + communicationEventIds + '"的数据项？').then(function () {
        return delEvent(communicationEventIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehr/event/export', {
        ...this.queryParams
      }, `event_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
